﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using FJASPX.Service;
using NHibernate.Criterion;

namespace PlugNT.Cms.Web
{
    public partial class Trades : System.Web.UI.Page
    {
        public long TotalCount = 0;

        public long TotalCount1 = 0;

        private static readonly ILog logger = LogManager.GetLogger(typeof(Trades));

        public IBuyService iBuyService { get; set; }
        public IProductService iProductService { get; set; }
        public IList<FJASPX.Domain.Buy> BuyList { get; set; }
        /// <summary>
        /// 热门推荐
        /// </summary>
        public IList<FJASPX.Domain.Buy> BuyList1 { get; set; }

        /// <summary>
        /// 热门产品
        /// </summary>
        public IList<FJASPX.Domain.Product> ProductList { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            Bind(); Bind1(); BindProduct();
        }


        private void Bind()
        {
            try
            {
                string key = null;
                if (!string.IsNullOrEmpty(Request.QueryString["key"]))
                {
                    key = Request.QueryString["key"].ToString();
                    ICriterion criterion = Expression.Like("Title", key, MatchMode.Anywhere);
                    List<Order> orders = new List<Order>();
                    orders.Add(new Order("TimeStamp", false));
                    this.BuyList = iBuyService.PagedQuery(criterion, orders, 0, 8, out TotalCount);
                }
                else
                {
                    this.BuyList = new List<FJASPX.Domain.Buy>(0);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }
        /// <summary>
        /// 热门推荐
        /// </summary>
        private void Bind1()
        {
            try
            {
                ICriterion criterion = null;
                List<Order> orders = new List<Order>();
                orders.Add(new Order("TimeStamp", false));
                this.BuyList1 = iBuyService.PagedQuery(criterion, orders, 0, 6, out TotalCount1);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }

        /// <summary>
        /// 热门产品
        /// </summary>
        private void BindProduct()
        {
            try
            {
                long totalCount = 0;
                ICriterion criterion = Expression.Eq("State", 1);
                List<Order> orders = new List<Order>();
                orders.Add(new Order("TimeStamp", false));
                this.ProductList = iProductService.PagedQuery(criterion, orders, 0, 8, out totalCount);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }

        public string ReplaceTitle(string title)
        {
            if (title != null)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["key"]))
                {
                    string key = Request.QueryString["key"].ToString();
                    title = title.Replace(key, "<font color=\"#ab3e05\">" + key + "</font>");
                }
                return title;
            }
            return string.Empty;
        }
    }
}